using UnityEngine;
using System.Collections;

public class HUDManager : MonoBehaviour 
{
	public static HUDManager Instance;
	
	public UISlider HPBar				= default( UISlider );
	public UISlider EnemyBar			= default( UISlider );
	
	public UISlider APBar				= default( UISlider );
	public UILabel APBarLabel			= default( UILabel );
	private int APBarValue				= 100;
	
	private float FromAPBarValue		= 0.0F;
	private float ToAPBarValue			= 0.0F;
	
	public UISprite[] skillTypes		= default( UISprite[] );
	public UILabel[] skillAPValue		= default( UILabel[] );
	public float[] skillDamage			= default( float[] );
	
	private int SKILL_ONE				= 0;
	private int SKILL_TWO				= 1;
	private int SKILL_THREE				= 2;
	private int SKILL_FOUR				= 3;
	
	public GameObject player;
	void Awake () 
	{
		Instance = this;
	}
	
	void UseSkill ()
	{
		if ( APBar.sliderValue != 0 && APBar.sliderValue >= ((float)int.Parse(skillAPValue[int.Parse(UIButtonMessage.currentCaller)].text) * 0.01F))
		{
			
			switch ( int.Parse(UIButtonMessage.currentCaller) )
			{
			case 0:
				
				BattleManager.Instance.PlayerSkill1 ();
				break;
				
			case 1:
				BattleManager.Instance.PlayerSkill2 ();
				break;
				
			case 2:
				BattleManager.Instance.PlayerSkill3 ();
				break;
				
			case 3:
				BattleManager.Instance.PlayerSkill4 ();
				break;
			}
			
			APBar.sliderValue -= ((float)int.Parse(skillAPValue[int.Parse(UIButtonMessage.currentCaller)].text) * 0.01F);
			
//			ToAPBarValue = APBar.rawValue - ((float)int.Parse(skillAPValue[int.Parse(UIButtonMessage.currentCaller)].text) * 0.01F);
//			StartCoroutine ( LerpSliderValue ( APBar , ToAPBarValue, false ) );	
			
			UpdateAPBarValue ( false );
		}
		
//		UpdateEnemyBarValue ( 50.0f );
	}
	
	#region CONSUMABLES
	void UseAPConsumables ()
	{
		if ( APBar.sliderValue < 1 )
		{
			APBar.sliderValue += 0.1F;
			//StartCoroutine ( LerpSliderValue ( APBar , (APBar.sliderValue + 0.1F), true ) );
			UpdateAPBarValue ( true );
		}
	}
	
	void UseHPConsumables ()
	{
		if ( HPBar.sliderValue < 1 )
		{
			HPBar.sliderValue += 0.1F;
		}
	}
	#endregion
	
	void UpdateAPBarValue ( bool isIncreasing )
	{
		if ( isIncreasing )
		{
			APBarValue += 10;
		}
		else
		{
			APBarValue -= int.Parse(skillAPValue[int.Parse(UIButtonMessage.currentCaller)].text);
		}
			APBarLabel.text = APBarValue + "/100";
	}
	
	#region ENEMY
	public void UpdateEnemyBarValue ( float damage )
	{
//		float test;
//		test = (damage * 0.01F);
//		
//		Debug.Log(test);
//		EnemyBar.sliderValue -= test;
		EnemyBar.sliderValue -= (damage * 0.01F);
	}
	
	#endregion
	
	//not fix yet
	IEnumerator LerpSliderValue ( UISlider slider, float toValue, bool isIncreasing )
	{
		float testval;
		if ( isIncreasing )
		{
			while( slider.sliderValue < toValue  )
			{
				Debug.Log(slider.sliderValue);
				slider.sliderValue = Mathf.Lerp(slider.sliderValue, toValue, slider.sliderValue);
				yield return 0;
			}
		}
		else
		{
			while( slider.rawValue > toValue  )
			{
				slider.sliderValue = Mathf.Floor(Mathf.Lerp(slider.rawValue, toValue, toValue));
				yield return 0;
			}
		}
	}
	
	
	

	
}
